﻿// ----------------------------------------------------------------------------------
// Microsoft Innovation Center Mobility Soluctions
// 
// Copyright (c) MICMS. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// ----------------------------------------------------------------------------------

using System;
using System.ComponentModel;
using System.Device.Location;
using System.Threading;
using System.Windows;

namespace Zombsquare
{
    public partial class MapPage: INotifyPropertyChanged
    {
        private GeoCoordinateWatcher watcher; 
     
        public bool trackingOn = true;
        
        private void GetLocation()
        {
            GeoCoordinateWatcher gg = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
            gg.MovementThreshold = 10;
        }
      
        private void InitializeGPS()
        {            
            watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High); 
            watcher.MovementThreshold = 10.0f;

            watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_statusChanged);
            watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
            
            new Thread(startLocServInBackground).Start();
        }

        private void startLocServInBackground()
        {
            watcher.TryStart(true, TimeSpan.FromSeconds(60));
        }

        private void watcher_statusChanged(object sender, GeoPositionStatusChangedEventArgs e)
        {
            switch (e.Status)
            {
                case GeoPositionStatus.Disabled:
                    if (watcher.Permission == GeoPositionPermission.Denied)
                    {
                        // the user has disabled LocServ on their device.
                        statusTextBlock.Text = "Tiene el servicio de localización desactivado.";
                    }
                    else
                    {
                        statusTextBlock.Text = "El servicio de localiación no funciona.";
                    }
                    break;

                case GeoPositionStatus.Initializing:                    
                    statusTextBlock.Text = "Actualizando localización...";
                    break;

                case GeoPositionStatus.NoData:                    
                    statusTextBlock.Text = "No hay una localización válida.";
                    break;

                case GeoPositionStatus.Ready:                    
                    statusTextBlock.Text = "Localización válida.";
                    break;
            }
        }

        private void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            if (trackingOn)
            {
                Center = e.Position.Location;
                App.ViewModel.CurrentLocation = e.Position.Location;

                if (App.ViewModel.Pushpins.Contains(pmLocation))
                {
                    App.ViewModel.Pushpins.Remove(pmLocation);
                    pmLocation.Location = e.Position.Location;                    
                }
                else
                {
                    pmLocation.Location = e.Position.Location;                    
                }
                //TODO: App.ViewModel.Pushpins.Add(pmLocation);
            }
        }

        private void trackMe_Click(object sender, RoutedEventArgs e)
        {
            if (trackingOn)
            {
                trackingOn = false;
            }
            else
            {             
                trackingOn = true;
            }
        }
    }
}
